package cn.demengsite.controller;

import cn.demengsite.model.Attribute;
import cn.demengsite.model.BaseInfo;
import cn.demengsite.model.VendorProduct;
import cn.demengsite.service.AttributeService;
import cn.demengsite.service.VendorService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.logging.Logger;

@Controller
public class WebController {

    private   final Logger logger = Logger.getLogger(WebController.class.getName());

    @Autowired
    private VendorService vendorService;

    @Autowired
    private AttributeService attributeService;

    @GetMapping("/web/showWebPage")
    public String showReportPage(){
        logger.info("come in web");
        return "webAdmin/webadmin";
    }
    @GetMapping("/web/showVendorPage")
    public String showVendorPage(){
        logger.info("come in showVenderPage");
        return "webAdmin/vendor";
    }
    @GetMapping("/web/getAllVendorProduct")
    @ResponseBody
    public String getAllVendorProduct(){
        logger.info("come in getAllVendorProduct");
        BaseInfo baseInfo = vendorService.getAllVendorProduct();
        return JSON.toJSONString(baseInfo.getObject());
    }

    @PostMapping("/web/newVendorProduct")
    @ResponseBody
    public String newVendorProduct(VendorProduct vendorProduct){
        logger.info("come in getAllVendorProduct");
        BaseInfo baseInfo = vendorService.newVendorProduct(vendorProduct);
        return JSON.toJSONString(baseInfo.getObject());
    }
    @PostMapping("/web/updateVendorProduct")
    @ResponseBody
    public String updateVendorProduct(VendorProduct vendorProduct){
        logger.info("come in updateVendorProduct");
        BaseInfo baseInfo = vendorService.updateVendorProduct(vendorProduct);
        return JSON.toJSONString(baseInfo.getObject());
    }
    @GetMapping("/web/showAttrPage")
    public String showAttrPage(){
        logger.info("come in showVenderPage");
        return "webAdmin/attribute";
    }
    @GetMapping("/web/getAllAttribute")
    @ResponseBody
    public String getAllAttribute(){
        logger.info("come in getAllVendorProduct");
        BaseInfo baseInfo = attributeService.getAllAttribute();
        return JSON.toJSONString(baseInfo.getObject());
    }

    @PostMapping("/web/newAttribute")
    @ResponseBody
    public String newAttribute(Attribute attribute){
        logger.info("come in getAllVendorProduct");
        BaseInfo baseInfo = attributeService.newAttribute(attribute);
        return JSON.toJSONString(baseInfo.getObject());
    }
    @PostMapping("/web/updateAttribute")
    @ResponseBody
    public String updateAttr(Attribute attribute){
        logger.info("come in updateVendorProduct");
        BaseInfo baseInfo = attributeService.updateAttribute(attribute);
        return JSON.toJSONString(baseInfo.getObject());
    }
}
